package com.bawei.property.view

import android.graphics.BitmapFactory
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.mvi.BaseMviActivity
import com.bawei.property.R
import com.bawei.property.databinding.ActivityParkDetailBinding
import com.bawei.property.entity.ParkValue
import com.google.vr.sdk.widgets.pano.VrPanoramaEventListener
import com.google.vr.sdk.widgets.pano.VrPanoramaView
import com.google.vr.sdk.widgets.video.VrVideoView
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

@Route(path = RoutePath.PATH_PARK_DETAIL_PAGE)
@AndroidEntryPoint
class ParkDetailActivity : BaseMviActivity<ActivityParkDetailBinding>() {

    @Autowired
    lateinit var parkEntity: ParkValue

    override fun getLayoutId(): Int = R.layout.activity_park_detail

    override fun initViews() {
        ARouter.getInstance().inject(this)
        mDataBinding.includeTitle.tvTitle.text = "车位预览"
        mDataBinding.includeTitle.ivBack.setOnClickListener {
            this@ParkDetailActivity.finish()
        }
        //修改车位信息
        mDataBinding.tvSelect.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_PARK_SELECT_PAGE).withSerializable("parkEntity", parkEntity).navigation()
        }
        //设置VR预览相关参数
        mDataBinding.vrPanoramaView.setInfoButtonEnabled(false) //隐藏信息按钮
        mDataBinding.vrPanoramaView.setFullscreenButtonEnabled(false) //隐藏全屏按钮
        //VR加载事件监听
        mDataBinding.vrPanoramaView.setEventListener(object : VrPanoramaEventListener(){
            override fun onLoadSuccess() {
               Log.e("======", "Vr onLoadSuccess")
            }

            override fun onLoadError(errorMessage: String?) {
                Log.e("======", "Vr onLoadError: $errorMessage")
            }
        })

        //本地模拟车位图片加载
        loadLocalVrImage()
    }

    private fun loadLocalVrImage(){
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                //从assets中读取图片
//                var inputStream = assets.open("park.jpg")
//                var bitmap = BitmapFactory.decodeStream(inputStream)
//                var options = VrPanoramaView.Options()
//                options.inputType = VrPanoramaView.Options.TYPE_MONO //指定VR预览类型
//                mDataBinding.vrPanoramaView.loadImageFromBitmap(bitmap, options)
                //bitmap.recycle()
                var videoOptions = VrVideoView.Options()
                videoOptions.inputType =  VrVideoView.Options.TYPE_MONO //指定VR预览类型
                mDataBinding.vrVideoView.loadVideoFromAsset("video.mp4", videoOptions)
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                cancel()
            }
        }
    }

    override fun onResume() {
        mDataBinding.vrPanoramaView.resumeRendering()
        super.onResume()
    }

    override fun onPause() {
        mDataBinding.vrPanoramaView.pauseRendering()
        super.onPause()
    }

    override fun onDestroy() {
        mDataBinding.vrPanoramaView.shutdown()
        super.onDestroy()
    }

}